package com.xiaoge.biz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoge.auth.annotation.ControllerEndpoint;
import com.xiaoge.biz.entity.*;
import com.xiaoge.biz.service.*;
import com.xiaoge.biz.vo.OutStockVo;
import com.xiaoge.biz.vo.GetOutStockVo;
import com.xiaoge.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xiaoge
 * @since 2021-03-21
 */
@CrossOrigin
@RestController
@RequestMapping("/outStock")
public class OutStockController {

    @Autowired
    private OutStockService outStockService;
    @Autowired
    private ConsumerService consumerService;
    @Autowired
    private ProductService productService;

    /**
     * 分页查询入库列表
     *
     * @param current
     * @param size
     * @param outStockVo
     * @return
     */
    @ApiOperation(value = "分页查询入库列表")
    @PostMapping("/findOutStockList")
    public Result findOutStockList(@RequestParam(defaultValue = "1") Integer current,
                                  @RequestParam(defaultValue = "6") Integer size,
                                  @RequestBody OutStockVo outStockVo) {
        //取得分页条件
        Page<OutStock> page = new Page<>(current, size);
        //获得wragger
        QueryWrapper<OutStock> queryWrapper = getWrapper(outStockVo);

        //执行查询
        IPage<OutStock> productPage = outStockService.findOutStockList(page, queryWrapper);

        long total = productPage.getTotal();
        List<OutStock> records = productPage.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }

    /**
     * 入库申请
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "入库单申请失败", operation = "入库申请")
    @ApiOperation(value = "入库申请")
    @PostMapping("/addOutStock")
    public Result addOutStock(@RequestBody GetOutStockVo getOutStockVo) {
        //新增来源
        if (getOutStockVo.getConsumerId() == null) {
            Consumer consumer = new Consumer();
            consumer.setAddress(getOutStockVo.getAddress());
            consumer.setContact(getOutStockVo.getContact());
            consumer.setName(getOutStockVo.getName());
            consumer.setPhone(getOutStockVo.getPhone());
            consumer.setSort(getOutStockVo.getSort());
            Consumer consumer1 = consumerService.add(consumer);
            getOutStockVo.setConsumerId(consumer1.getId());
        }

        outStockService.addOutStock(getOutStockVo);
        return Result.ok();
    }

    /**
     * 物资出库单详细
     *
     * @param
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "查询物资出库单详细失败", operation = "物资出库单详细")
    @ApiOperation(value = "物资出库单详细")
    @PostMapping("/detail")
    public Result detail(@RequestParam String outNum,
                         @RequestParam Long consumerId,
                         @RequestParam(defaultValue = "1") Integer current,
                         @RequestParam(defaultValue = "3") Integer size) {
        //取得分页条件
        Page<Product> page = new Page<>(current, size);

        //执行查询
        IPage<Product> productPage = productService.getOutDetails(page, outNum);

        Consumer consumer = consumerService.getById(consumerId);

        long total = productPage.getTotal();
        List<Product> records = productPage.getRecords();
        return Result.ok().data("total", total).data("records", records).data("consumer", consumer);
    }

    /**
     * 恢复入库清单
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "恢复入库清单失败", operation = "恢复入库清单")
    @ApiOperation(value = "恢复入库清单")
    @PostMapping("/backOutStock")
    public Result backOutStock(Long id) {
        OutStock outStock = new OutStock();
        outStock.setId(id);
        outStock.setStatus(0);
        outStockService.updateById(outStock);
        return Result.ok();
    }

    /**
     * 移至回收站
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "移至回收站失败", operation = "移至回收站")
    @ApiOperation(value = "移至回收站")
    @PostMapping("/removeOutStock")
    public Result removeOutStock(Long id) {
        OutStock outStock = new OutStock();
        outStock.setId(id);
        outStock.setStatus(1);
        outStockService.updateById(outStock);
        return Result.ok();
    }

    /**
     * 审核通过
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "审核失败", operation = "审核通过")
    @ApiOperation(value = "审核通过")
    @PostMapping("/publishOutStock")
    public Result publishOutStock(Long id,String outNum) {
        outStockService.publishOutStock( id, outNum);
        return Result.ok();
    }

    /**
     * 删除入库清单
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除入库清单失败", operation = "删除入库清单")
    @ApiOperation(value = "删除入库清单")
    @DeleteMapping("/delOutStock")
    public Result delOutStock(Long id,String outNum) {
        outStockService.delete(id,outNum);
        return Result.ok();
    }

    private QueryWrapper<OutStock> getWrapper(OutStockVo outStockVo) {
        QueryWrapper<OutStock> queryWrapper = new QueryWrapper<>();
        if (outStockVo != null) {

            //查询每个条件
            if (!ObjectUtils.isEmpty(outStockVo.getStatus())) {
                queryWrapper.eq("status", outStockVo.getStatus());
            }
            if (!ObjectUtils.isEmpty(outStockVo.getOutNum())) {
                queryWrapper.like("out_num", outStockVo.getOutNum());
            }
            if (!ObjectUtils.isEmpty(outStockVo.getType())) {
                queryWrapper.eq("type", outStockVo.getType());
            }
            if (!ObjectUtils.isEmpty(outStockVo.getStartTime())) {
                queryWrapper.ge("o.create_time", outStockVo.getStartTime());
            }
            if (!ObjectUtils.isEmpty(outStockVo.getEndTime())) {
                queryWrapper.le("o.create_time", outStockVo.getEndTime());
            }
        }
        return queryWrapper;
    }
}
